package service

import (
	"ollama-go/dto"
	"ollama-go/entity"
	"ollama-go/query"
	"ollama-go/vo"
	"strings"
	"time"

	"github.com/androidsr/sc-go/model"
	"github.com/androidsr/sc-go/sbuilder"
	"github.com/androidsr/sc-go/sc"
	"github.com/androidsr/sc-go/sgorm"
	"github.com/androidsr/sc-go/sno"
	"gorm.io/gorm"
)

var tsTaskStepService *TsTaskStepService

type TsTaskStepService struct {
}

func NewTsTaskStepService() *TsTaskStepService {
	if tsTaskStepService == nil {
		tsTaskStepService = new(TsTaskStepService)
	}
	return tsTaskStepService
}

// 使用id获取数据
func (m TsTaskStepService) Get(id string) *vo.TsTaskStepVO {
	data := new(entity.TsTaskStep)
	data.Id = id
	err := sgorm.DB.GetOne(data)
	if err != nil {
		return nil
	}
	return sc.Copy[vo.TsTaskStepVO](data)
}

// 保存数据
func (m TsTaskStepService) Save(dto *dto.TsTaskStepDTO) error {
	data := sc.Copy[entity.TsTaskStep](dto)
	data.Id = sno.GetString()
	data.UpdateTime = time.Now()
	if data.ExecBy < 1 {
		maxSortBy := 0
		sgorm.DB.SelectSQL(&maxSortBy, `select max(exec_by) from ts_task_step where task_id = ? `, data.TaskId)
		data.ExecBy = maxSortBy + 1
	}
	err := sgorm.DB.Insert(data)
	return err
}

// 更新数据
func (m TsTaskStepService) UpdateById(dto *dto.TsTaskStepDTO) error {
	dbEntity := new(entity.TsTaskStep)
	dbEntity.Id = dto.Id
	sgorm.DB.GetOne(dbEntity)
	sc.CopyTo(dto, dbEntity)
	dbEntity.UpdateTime = time.Now()
	if dbEntity.ExecBy < 1 {
		maxSortBy := 0
		err := sgorm.DB.SelectSQL(&maxSortBy, `select max(exec_by) from ts_task_step where task_id = ? `, dbEntity.TaskId)
		if err != nil {
			return err
		}
		dbEntity.ExecBy = maxSortBy + 1
	}
	err := sgorm.DB.UpdateById(dbEntity).Error
	return err
}

// 删除数据
func (m TsTaskStepService) DeleteById(id string) error {
	err := sgorm.DB.DeleteById(entity.TsTaskStep{}, id)
	return err
}

// 分页列表查询
func (m TsTaskStepService) QueryPage(query *query.TsTaskStepQueryDTO) *model.PageResult {
	sql := `select b.name as task_id_name,a.* from ts_task_step a left join ts_task b on b.id = a.task_id where 1=1`
	data := make([]vo.TsTaskStepVO, 0)
	b := sbuilder.StructToBuilder(query, sql)
	sql, values := b.Build()
	sql += ` order by a.exec_by,id asc `
	return sgorm.DB.SelectPage(&data, query.BaseQueryDTO.Page, sql, values...)
}

// 分页下拉查询
func (m TsTaskStepService) QueryList(query *model.SelectQueryDTO) *model.PageResult {
	sql := `select a.id as value, a.name as label from ts_task_step a where 1=1 `
	data := make([]model.SelectVO, 0)
	b := sbuilder.Builder(sql)
	b.Like("CONCAT(a.id,a.name)", query.Label)
	for k, v := range query.Vars {
		b.Eq(k, v)
	}
	b.Multiple().Ors(b.In("a.id", query.Selected))
	sql, values := b.Build()
	if len(query.Selected) != 0 {
		sql += `order by a.id not in (` + strings.Join(query.Selected, ",") + `)`
	}
	return sgorm.DB.SelectPage(&data, &query.Page, sql, values...)
}

// 按任务ID查询
func (m TsTaskStepService) FindByTaskId(taskId string) ([]vo.TsTaskStepVO, error) {
	sql := `select * from ts_task_step a where 1=1 `
	data := make([]vo.TsTaskStepVO, 0)
	b := sbuilder.Builder(sql)
	b.Eq("a.task_id", taskId)
	sql, values := b.Build()
	sql += ` order by a.exec_by asc `
	err := sgorm.DB.SelectSQL(&data, sql, values...)
	if err != nil {
		return nil, err
	}
	return data, nil
}

func (m *TsTaskStepService) BatchAdd(taskId string, data *[]entity.TsTaskStep) error {
	q := entity.TsTaskStep{}
	sgorm.DB.Tx(func(tx *gorm.DB) error {
		err := tx.Where("task_id = ?", taskId).Delete(q).Error
		if err != nil {
			return err
		}
		for i, item := range *data {
			item.TaskId = taskId
			item.UpdateTime = time.Now()
			item.ExecBy = i + 1
			err = tx.Create(&item).Error
			if err != nil {
				return err
			}
		}
		return nil
	})
	return nil
}
